package com.zhuss.core.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.role.StartRoleBo;
import com.zhuss.bean.vo.core.role.DetailRoleVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.RoleDao;
import com.zhuss.core.db.entity.Role;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.OceanException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 创建角色 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("roleStart")
public class RoleStart extends AbstractProcessor<Object, StartRoleBo> implements Processor<Object, StartRoleBo> {

    @Autowired
    private RoleDao roleDao;
    
    @Autowired
	@Qualifier("roleRead")
	private RoleRead roleRead;
    
    @Autowired
    private RoleCheck roleCheck;
    
    /**
     * 校验
     */
    @Override
    public void check(StartRoleBo bo) throws OceanException {
    	// 当前角色不满足启用条件
		if (!roleCheck.checkStart(bo.getRoleId())) {
			throw new OceanException(ExceptionCode.当前角色不满足启用条件);
		}
    }
    
    /**
     * 操作方法
     * @throws OceanException 
     */
    @Override
    public Object process(StartRoleBo param) throws OceanException {
    	
    	Long roleId = param.getRoleId();
    	
    	Role record = new Role();
    	record.setRoleId(param.getRoleId());
    	record.setStatus(param.getStatus());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	roleDao.updateByPrimaryKeySelective(record);
    	
    	String name = ((DetailRoleVo) roleRead.detail(roleId)).getName();
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(StartRoleBo param) throws OceanException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.角色启用.key());
    	record.setTargetId(param.getRoleId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
